﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace TaDMS.Models
{
    public class DeviceCategoryRepository : Repository<DeviceCategory>
    {
        public DeviceCategoryRepository(DbContext model) : base(model)
        {
        }

        public override IEnumerable<DeviceCategory> GetData(out int? total, Func<IQueryable<DeviceCategory>, IOrderedQueryable<DeviceCategory>> orderBy, Expression<Func<DeviceCategory, bool>> filter = null, IList<Expression<Func<DeviceCategory, bool>>> multiFilter = null, string includeProperties = "", bool isPaging = false, int start = 0, int limit = 50)
        {
            try
            {
                IQueryable<DeviceCategory> query = base.DbSet;
                IEnumerable<DeviceCategory> result = null;

                if (filter != null)
                {
                    query = query.Where(filter);
                }
                if (multiFilter != null && multiFilter.Any())
                {
                    query = multiFilter.Aggregate(query, (current, expression) => current.Where(expression));
                }

                query = includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Aggregate(query, (current, includeProperty) => current.Include(includeProperty));

                if (isPaging)
                {
                    total = query.Count();

                    query = orderBy != null ? orderBy(query) : query;

                    query = query.Skip(start).Take(limit);

                    result = query.ToList().Select(t => new DeviceCategory { DeviceCategoryId = t.DeviceCategoryId, Desciptions = t.Desciptions, Name = t.Name, ParentDeviceCategoryId = t.ParentDeviceCategoryId });

                    //total = result.Count();
                }
                else
                {
                    total = null;
                }

                return result;

            }
            catch (Exception exception)
            {
                this.ErrorMessage = exception.Message;

                total = null;

                return null;
            }
        }

        public List<DeviceCategory> GetDeviceCategoryTree()
        {
            using (var db = new TaDMS())
            {
                var parentDeviceCategory = db.sp_DeviceCategory_GetRoot().ToList();

                var result = parentDeviceCategory.Select(rootResult => new DeviceCategory() { DeviceCategoryId = rootResult.DeviceCategoryId, Name = rootResult.Name, Desciptions = rootResult.Desciptions, ParentDeviceCategoryId = rootResult.ParentDeviceCategoryId }).ToList();

                GetList(result);

                return result;
            }
        }

        public override int? Delete(DeviceCategory entity)
        {
            try
            {
                using (var db = new TaDMS())
                {
                    return db.sp_DeviceCategory_Delete(entity.DeviceCategoryId);
                }
            }
            catch (Exception exception)
            {
                ErrorMessage = exception.Message;

                return null;
            }
        }


        /*Private method*/
        private void GetList(IEnumerable<DeviceCategory> source)
        {
            foreach (var DeviceCategory in source)
            {
                var newSource = new List<DeviceCategory>();
                using (var db = new TaDMS())
                {
                    var data = db.sp_DeviceCategory_GetChildren(DeviceCategory.DeviceCategoryId).AsEnumerable();
                    newSource.AddRange(data.Select(result => new DeviceCategory() { DeviceCategoryId = result.DeviceCategoryId, Name = result.Name, Desciptions = result.Desciptions, ParentDeviceCategoryId = result.ParentDeviceCategoryId }));
                }

                DeviceCategory.DeviceCategory1 = newSource;

                this.GetList(newSource);
            }
        }
    }
}